package com.mxin.ui.test

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mxin.ui.R
import com.mxin.ui.base.BaseActivity
import com.mxin.ui.list.BaseListViewHelper
import com.mxin.ui.list.DataSource
import com.mxin.ui.list.toPageData
import com.mxin.ui.loading.Gloading
import com.mxin.ui.loading.IndicatorAdapter
import com.wang.avi.AVLoadingIndicatorView


class LoadingViewConfigActivity : BaseActivity() {

    private val listViewHelper = BaseListViewHelper<IndicatorAdapter.Indicator>()
    private lateinit var recyclerView: RecyclerView

    override fun setContentView() {
        setContentView(R.layout.ui_activity_test_aty)
    }

    override fun showLoadingView(): View? = recyclerView


    override fun initView() {
        val titleStub = findViewById<ViewStub>(R.id.titleView)
        setTitleView(titleStub, "加载动画")
        val contentView = findViewById<ViewGroup>(R.id.layout_content)
        recyclerView = listViewHelper.bindListView(contentView){
            it.layoutManager = GridLayoutManager(this,4)
            it.adapter = AviAdapter(mutableListOf()).apply {
                setOnItemClickListener { i, s ->
                    Gloading.initDefault(IndicatorAdapter(s))
                    Toast.makeText(getContext(), "设置加载动画"+s.name, Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    override fun initData() {
        listViewHelper.emit(initListData().toPageData())
    }

    override fun onLoadRetry() {
        initData()
    }

    private fun initListData():List<IndicatorAdapter.Indicator>{
        val data = mutableListOf<IndicatorAdapter.Indicator>()
        data.add(IndicatorAdapter.Indicator.BallPulseIndicator)
        data.add(IndicatorAdapter.Indicator.BallGridPulseIndicator)
        data.add(IndicatorAdapter.Indicator.BallClipRotateIndicator)
        data.add(IndicatorAdapter.Indicator.BallClipRotatePulseIndicator)
        data.add(IndicatorAdapter.Indicator.SquareSpinIndicator)
        data.add(IndicatorAdapter.Indicator.BallClipRotateMultipleIndicator)
        data.add(IndicatorAdapter.Indicator.BallPulseRiseIndicator)
        data.add(IndicatorAdapter.Indicator.BallRotateIndicator)
        data.add(IndicatorAdapter.Indicator.CubeTransitionIndicator)
        data.add(IndicatorAdapter.Indicator.BallZigZagIndicator)
        data.add(IndicatorAdapter.Indicator.BallZigZagDeflectIndicator)
        data.add(IndicatorAdapter.Indicator.BallTrianglePathIndicator)
        data.add(IndicatorAdapter.Indicator.BallScaleIndicator)
        data.add(IndicatorAdapter.Indicator.LineScaleIndicator)
        data.add(IndicatorAdapter.Indicator.LineScalePartyIndicator)
        data.add(IndicatorAdapter.Indicator.BallScaleMultipleIndicator)
        data.add(IndicatorAdapter.Indicator.BallPulseSyncIndicator)
        data.add(IndicatorAdapter.Indicator.BallBeatIndicator)
        data.add(IndicatorAdapter.Indicator.LineScalePulseOutIndicator)
        data.add(IndicatorAdapter.Indicator.LineScalePulseOutRapidIndicator)
        data.add(IndicatorAdapter.Indicator.BallScaleRippleIndicator)
        data.add(IndicatorAdapter.Indicator.BallScaleRippleMultipleIndicator)
        data.add(IndicatorAdapter.Indicator.BallSpinFadeLoaderIndicator)
        data.add(IndicatorAdapter.Indicator.LineSpinFadeLoaderIndicator)
        data.add(IndicatorAdapter.Indicator.TriangleSkewSpinIndicator)
        data.add(IndicatorAdapter.Indicator.PacmanIndicator)
        data.add(IndicatorAdapter.Indicator.BallGridBeatIndicator)
        data.add(IndicatorAdapter.Indicator.SemiCircleSpinIndicator)
        return data
    }


}

class AviAdapter(val data:MutableList<IndicatorAdapter.Indicator>) : RecyclerView.Adapter<IndicatorViewHolder>(), DataSource<IndicatorAdapter.Indicator>, View.OnClickListener {

    private var onItemClickListener:((Int,IndicatorAdapter.Indicator)->Unit)?=null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IndicatorViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.ui_item_test_indicator, parent, false)
        view.setOnClickListener(this)
        return IndicatorViewHolder(view)
    }

    override fun onBindViewHolder(holder: IndicatorViewHolder, position: Int) {
        holder.avi.setIndicator(data[position].name)
        holder.itemView.setTag(R.id.tag_holder, holder)
    }

    override fun onViewAttachedToWindow(holder: IndicatorViewHolder) {
        super.onViewAttachedToWindow(holder)
        if(!holder.avi.isShown) {
            holder.avi.show()
        }
    }

    override fun onViewDetachedFromWindow(holder: IndicatorViewHolder) {
        super.onViewDetachedFromWindow(holder)
        if(holder.avi.isShown){
            holder.avi.hide()
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun addData(t: List<IndicatorAdapter.Indicator>?) {
        val positionStart = data.size
        data.addAll(t?: emptyList())
        notifyItemRangeInserted(positionStart, data.size - positionStart)
    }

    override fun setData(t: List<IndicatorAdapter.Indicator>?) {
        data.clear()
        data.addAll(t?: emptyList())
        notifyDataSetChanged()
    }

    fun setOnItemClickListener(listener:(Int,IndicatorAdapter.Indicator)->Unit){
        onItemClickListener = listener
    }

    override fun onClick(v: View?) {
        val holder = v?.getTag(R.id.tag_holder)
        if(holder is RecyclerView.ViewHolder){
            val position = holder.adapterPosition
            onItemClickListener?.invoke(position, data[position])
        }
    }

}

class IndicatorViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){

    val avi: AVLoadingIndicatorView = itemView.findViewById(R.id.avi)


}
